//
//  ViewController.m
//  23-QQ音乐MV作业讲解
//
//  Created by mafeng on 16/8/15.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking/AFNetworking.h"
#import "MVModel.h"
#import "MVData.h"
#import "MVContents.h"
#import "MVTableList.h"
#import "MVConllectionList.h"
#import "MVTableViewCell.h"
#import "MVSingers.h"

#define kTableViewCellReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *qq_MVTableView;
@property (nonatomic, strong) MVModel *mvModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
}

- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        // 将jsonp1替换
        dataString = [dataString stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
        dataString = [dataString substringToIndex:dataString.length - 1];
        NSData *finalData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:finalData options:NSJSONReadingMutableContainers error:&error];
        
        //NSLog(@"******%ld", _mvModel.code);
        NSLog(@"|||||||%@", result);
        self.mvModel = [MVModel modelWithDic:result];
        [_qq_MVTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _mvModel.data.contents.list.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.mvListArray = _mvModel.data.contents.list[indexPath.section].mvlist;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"最新";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
